const axios = require("axios")
const fs = require("fs")
const path = require("path")

const url =
  "https://zzcenter-hr-zzcenterhr-attendance-attendance.entry.ctbiyi.com/api/myatt/selectMonthAllTwo?openMonth=&endMonth=&currentPage=1&pagesize=100&userCode=YG"

const totalFile = []

const requestList = {
  [Symbol.asyncIterator]() {
    return {
      i: 1000,
      next() {
        while (this.i++ < 3000) {
          setTimeout(() => {
            return axios({
              url: `${url}${this.i.toString().padStart(6, "0")}`,
              method: "post",
            })
          }, 200)
        }
        return Promise.resolve({ done: true })
      },
    }
  },
}

const grab = async () => {
  for await (request of requestList) {
    console.log(request)
    // totalFile.push(request)
  }
}

// grab()
// console.log(totalFile)

for (let i = 5000; i <= 6000; ++i) {
  axios({
    url: `${url}${i.toString().padStart(6, "0")}`,
    method: "post",
  })
    .then(res => {
      const { data } = res.data
      if (typeof data === "object") {
        const { result } = data
        fs.writeFile(
          `./peopleInfo/${result[0].usercode}-${result[0].username}-${result[0].id}.json`,
          JSON.stringify(result[0]),
          { encoding: "utf-8" },
          err => {
            if (err) throw new Error(err.message)
          }
        )
      }
      if (i === 3000) {
        console.log(totalFile)
      }
    })
    .catch(err => {
      console.log(err)
    })
}
